import type {
    MeetingRoom,
    RoomReservations,
    MeetingRoomSearchParams,
} from "../types/room";
import type { Requirement } from "../types/reservation";
import { request } from "@/service/request/request";

/**
 * 添加会议室
 * @param room
 * @returns
 */
export const addMeetingRoom = async (room: MeetingRoom) => {
    try {
        return await request<MeetingRoom>(
            "/meeting_room/add_room",
            "post",
            room,
            { loading: false }
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 获取会议室数量
 * @returns
 */
export const getRoomCounts = async () => {
    try {
        return await request<number>(
            "/meeting_room/get_all_room_Count",
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 根据会议室id获取会议室的预约信息
 * @param roomId
 * @returns
 */
export const getRoomReservations = async (roomId: string) => {
    try {
        return await request<RoomReservations[]>(
            `/meeting_room/search_order_byMeetingRoom/${roomId}`,
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 根据会议室id获取会议室信息
 * @param roomId
 * @returns
 */
export const getMeetingRoomById = async (roomId: string) => {
    try {
        return await request<MeetingRoom>(
            `/meeting_room/get_room_by_id/${roomId}`,
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 分页获取会议室列表
 * @param params
 * @returns
 */
export const getMeetingRoomList = async (params: {
    page: number;
    size: number;
}) => {
    try {
        return await request<MeetingRoom[]>(
            "/meeting_room/get_page_room",
            "post",
            params,
            {
                loading: false,
            }
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 客户在预约界面筛选可预约的会议室
 * @param params Requirement 筛选条件
 * @returns
 */
export const filterUseableRooms = async (params: Requirement) => {
    try {
        return await request<MeetingRoom[]>(
            "/meeting_room/get_room_by_requirement",
            "post",
            params,
            {
                loading: false,
            }
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 管理员在会议室管理界面筛选会议室
 * @param params MeetingRoomSearchParams 筛选条件
 * @returns
 */
export const searchMeetingRooms = async (params: MeetingRoomSearchParams) => {
    try {
        return await request<MeetingRoom[]>(
            "/meeting_room/admin_search_room",
            "post",
            params,
            {
                loading: false,
            }
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 删除会议室
 * @param roomId
 * @returns
 */
export const deleteMeetingRoom = async (roomId: string) => {
    try {
        return await request<null>(
            `/meeting_room/delete_room/${roomId}`,
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 更新会议室
 * @param room 会议室信息
 * @returns
 */
export const updateMeetingRoom = async (room: MeetingRoom) => {
    try {
        return await request<MeetingRoom>(
            "/meeting_room/update_room",
            "post",
            room
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 获取正在使用的会议室
 * @returns
 */
export const getUsingRooms = async () => {
    try {
        return await request<string[]>(
            "/meeting_room/search_using_meetingRoom",
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};
